<?php
require_once 'base.php';

$tpl = new Template('t');
$tpl->setFile ( "main", "foradmin.html" );
$tpl->setBlock ( "main", "list", "lists" );

$db = conn();

// ------------- 编辑 -------------
$edit = fget('edit', 0);
if ($edit){
	$aid_edit = del_space(fget('aid', 0));
	switch ($edit){
		case "shield": // 屏蔽
			$sql_edit = "UPDATE article SET status=-1 WHERE aid='{$aid_edit}'";
			break;
		case "recover": // 恢复
			$sql_edit = "UPDATE article SET status=0 WHERE aid='{$aid_edit}'";
			break;
	}
	if ($db->query($sql_edit)){
		$tpl->setVar("alert_edit", "alert(\"操作成功！\")");
	}else{
		$tpl->setVar("alert_edit", "alert(\"操作失败！\")");
	}
}
// ------------- 显示list -------------
$sql = "SELECT COUNT(*) FROM article";
$rs=$db->query($sql, MYSQL_NUM);

if($rs[0][0] > 0){
	$page_str = pageft($rs[0][0],20);

	$sql = "SELECT * FROM article ORDER BY id DESC {$page_str['limit']}";
	$rs=$db->query($sql, MYSQL_ASSOC);
	$i = 0;
	foreach($rs as $v){
		// 屏蔽、恢复文章
		if ($v['status'] > -1){
			$edit_str = "<a href=\"?edit=shield&aid={$v['aid']}\">屏蔽</a>";
		}else{
			$edit_str = "<a href=\"?edit=recover&aid={$v['aid']}\">恢复</a>";
		}
		// 文章评论
		if ($v['comment'] > 0){
			$edit_str .= " | <a href=\"comment_edit.php?aid={$v['aid']}\">查看评论</a>";
		}else{
			$edit_str .= " | <a href=\"javascript: void(0);\">查看评论</a>";
		}
		// 行颜色
		$color_h = ($i++%2)>0 ? " class=\"color_h\"" : "";
		$tpl->setVar(array(
			"id" => $v['id'],
			"aid" => $v['aid'],
			"title" => $v['title'],
			"submit_time" => date("Y-m-d h:i:s", $v['submit_time']),
			"amount" => $v['amount'],
			"comment" => $v['comment'],
			"edit_str" => $edit_str,
			"color_h" => $color_h
		));

		$tpl->parse("lists", "list", true);
	}
	$tpl->setVar("page_str", $page_str["str"]);
}

$db->close();

$tpl->p("main");
?>